﻿/* 
 * Godtear.com
 * QQ:bzure@189.cn
 *Author:Nathan Liu
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bzure.Security.Permissions
{
    [Serializable]
    public class Droit : IDroit
    {
        public Droit(Type type)
            : this(type.FullName)
        {
        }
        public Droit(string guid)
        {
            _accessTypes = new List<IAccessCode>();
            _guid = guid;
            this.Description = string.Empty;
        }
        public Droit(string guid, params IAccessCode[] AccessCodes)
            : this(guid)
        {
            foreach (IAccessCode at in AccessCodes)
                this.AddAccessCode(at);
        }
        public Droit(Type type, params IAccessCode[] AccessCodes)
            : this(type.FullName, AccessCodes)
        {
        }
        #region Implement IGuid interface
        private string _guid;
        public string Guid
        {
            get { return _guid; }
        }
        #endregion Implement IGuid interface
        #region Implement IDroit interface
        public string Description
        {
            get;
            set;
        }
        private IList<IAccessCode> _accessTypes;
        public IList<IAccessCode> AccessCodes
        {
            get { return _accessTypes; }
        }
        public bool Contains(IAccessCode AccessType)
        {
            foreach (IAccessCode at in _accessTypes)
                if (at.CodeValue == AccessType.CodeValue)
                    return true;
            return false;
        }
        public IDroit Copy()
        {
            Droit droit = new Droit(this.Guid.ToString());
            foreach (IAccessCode at in this._accessTypes)
                droit.AddAccessCode(at.Copy());
            return droit;
        }
        public bool IsSubsetOf(IDroit Droit)
        {
            foreach (IAccessCode at in this._accessTypes)
                if (!Droit.Contains(at))
                    return false;
            return true;
        }
        #endregion Implement IDroit interface

        public void AddAccessCode(IAccessCode AccessType)
        {
            bool find = false;
            foreach (IAccessCode at in _accessTypes)
                if (at.CodeValue == AccessType.CodeValue)
                    find = true;
            if (!find)
                _accessTypes.Add(AccessType);
        }
        public void RemoveAccessCode(IAccessCode AccessType)
        {
            foreach (IAccessCode at in _accessTypes)
                if (at.CodeValue == AccessType.CodeValue)
                    _accessTypes.Remove(at);
        }
        public override bool Equals(object obj)
        {
            if (obj is Droit)
            {
                Droit oDroit = obj as Droit;
                bool same = true;
                same = same && this._guid.CompareTo(oDroit._guid) == 0;
                same = same && this._accessTypes.Count == oDroit._accessTypes.Count;
                foreach (IAccessCode at in this._accessTypes)
                    same = same && oDroit.Contains(at);
                return same;
            }
            return base.Equals(obj);
        }
        public override int GetHashCode()
        {
            return this._guid.GetHashCode();
        }
    }
}
